import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: fei.wu
 * @Email: fei.wu@rograndec.com
 * @CreateDate: 2018/12/18
 * @Version: 1.0
 */
public class ForThreadPool {

    public static void main(String[] args) throws InterruptedException {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(i);
        }

        ExecutorService pool = new ThreadPoolExecutor(10, 20,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024), new ThreadPoolExecutor.AbortPolicy());
        long start = System.currentTimeMillis();
        for (Integer i : list) {
            Callable<Integer> run = new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    System.out.println(i);
                    TimeUnit.SECONDS.sleep(1);
                    return i;
                }
            };
            pool.submit(run);
        }
        pool.shutdown();
        while (true) {
            if (pool.isTerminated()) {
                System.out.println("使用线程池一共执行：" + String.valueOf(System.currentTimeMillis() - start) + "ms");
                break;
            }
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
}
